package com.eric.windows.imgDispose;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.dd.CircularProgressButton;
import com.eric.windows.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.Timer;
import java.util.TimerTask;

@EActivity(R.layout.activity_progess_test)
public class ProgessTestActivity extends Activity {
    @ViewById(R.id.progress_btn)
    CircularProgressButton progressButton;
    @ViewById(R.id.progress_btn2)
    CircularProgressButton progressButton2;
    @ViewById(R.id.progress_btn3)
    CircularProgressButton progressButton3;
    @ViewById(R.id.progress_btn4)
    CircularProgressButton progressButton4;

    private Timer timer;

    private int progess = 0;
    private int progess2 = 0;

    @AfterViews
    void initViews(){
        progressButton.setIndeterminateProgressMode(true);
//        progressButton2.setIndeterminateProgressMode(true);

    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 21:
                    if (progess <= 100){
                        progess += 5;
                        progressButton.setProgress(progess);
                    }
                    break;
                case 31:
                    if (progess2 <= 50){
                        progess2 += 5;
                        progressButton2.setProgress(progess2);
                    }else {
                        progressButton2.setProgress(-1);
                    }
                    break;

            }

            return false;
        }
    });


    @Click({R.id.progress_btn,R.id.progress_btn2,R.id.progress_btn3,R.id.progress_btn4})
    void onClick(View v){
        switch (v.getId()){
            case R.id.progress_btn:
                progressButton.setProgress(0);
                progess = 0;
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(21);
                    }
                },0,500);

                break;
            case R.id.progress_btn2:
                progressButton2.setProgress(0);
                progess2 = 0;
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(31);
                    }
                },0,500);

                break;
            case R.id.progress_btn3:
                simulateSuccessProgress(progressButton3);
                break;
            case R.id.progress_btn4:
                if (progressButton4.getProgress() == 0) {
                    progressButton4.setProgress(100);
                } else {
                    progressButton4.setProgress(0);
                }

                break;

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null){
            timer.cancel();
        }

    }

    private void simulateSuccessProgress(final CircularProgressButton button) {
        ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100);
        widthAnimation.setDuration(1500);
        widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                button.setProgress(value);
            }
        });
        widthAnimation.start();
    }

    private void simulateErrorProgress(final CircularProgressButton button) {
        ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 99);
        widthAnimation.setDuration(1500);
        widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                button.setProgress(value);
                if (value == 99) {
                    button.setProgress(-1);
                }
            }
        });
        widthAnimation.start();
    }



}
